package com.jf.identity.util;

import com.jf.identity.dao.vis.ExamInfoDao;
import com.jf.identity.dao.vis.ExamSessionDao;
import com.jf.identity.entity.ExamInfo;
import com.jf.identity.entity.ExamSessionInfo;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by x on 2018/5/18.
 */
@Component
public class ExamIdAndExamSessionIdUtil {
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;
    @Resource(name = "redisTemplate")
    private ListOperations<String, Object> listOperations;
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOperations;
    @Resource
    private ExamInfoDao examInfoDao;
    @Resource
    private ExamSessionDao examSessionDao;

    public List<ExamInfo> getExamIdCatch(){
        String key = "cdm_examId";
        List<ExamInfo> list;
        if (redisTemplate.hasKey(key)) {
            list = (List<ExamInfo>)valueOperations.get(key);
        }else {
            list = examInfoDao.getAllExamIds();
            valueOperations.set(key,list, RedisConstant.M_FREQUENCY_TIME_OUT, TimeUnit.SECONDS);
        }
        return list;
    }

    public List<ExamSessionInfo> getExamSessionIdCatch(){
        String key = "cdm_examSessionId";
        List<ExamSessionInfo> list;
        if (redisTemplate.hasKey(key)) {
            list = (List<ExamSessionInfo>)valueOperations.get(key);
        }else {
            list = examSessionDao.getAllExamSessionIds();
            valueOperations.set(key,list, RedisConstant.M_FREQUENCY_TIME_OUT, TimeUnit.SECONDS);
        }
        return list;
    }

}
